﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Collections.ObjectModel;

namespace BambolettaDB
{
    public class AddRequestVM: BaseVM
    {
        #region Properties and Fields
        private RequestVM _EditVM = null;

        private RequestVM _Request = new RequestVM();
        public RequestVM Request
        {
            get { return _Request; }
            set { _Request = value; NotifyPropertyChanged("Request"); }
        }

        private ObservableCollection<StringItemVM> _Hair = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Hair
        {
            get { return _Hair; }
            set { _Hair = value; NotifyPropertyChanged("Hair"); }
        }

        private ObservableCollection<StringItemVM> _HairTypes = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> HairTypes
        {
            get { return _HairTypes; }
            set { _HairTypes = value; NotifyPropertyChanged("HairTypes"); }
        }

        private ObservableCollection<StringItemVM> _Eyes = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Eyes
        {
            get { return _Eyes; }
            set { _Eyes = value; NotifyPropertyChanged("Eyes"); }
        }

        private ObservableCollection<StringItemVM> _Skin = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Skin
        {
            get { return _Skin; }
            set { _Skin = value; NotifyPropertyChanged("Skin"); }
        }

        private ObservableCollection<StringItemVM> _Gender = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Gender
        {
            get { return _Gender; }
            set { _Gender = value; NotifyPropertyChanged("Gender"); }
        }

        private ObservableCollection<StringItemVM> _Types = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Types
        {
            get { return _Types; }
            set { _Types = value; NotifyPropertyChanged("Types"); }
        }

        private ObservableCollection<StringItemVM> _Status = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Status
        {
            get { return _Status; }
            set { _Status = value; NotifyPropertyChanged("Status"); }
        }

        private ObservableCollection<StringItemVM> _Locations = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Locations
        {
            get { return _Locations; }
            set { _Locations = value; NotifyPropertyChanged("Locations"); }
        }
        #endregion

        #region Commands
        private DelegateCommand _AddCommand;
        public ICommand AddCommand
        {
            get { return _AddCommand; }
        }
        public void AddExecuted(object parameter)
        {
            if (Request.ID == -1)
            {
                DBCommands.InsertRequest(Request);
                MainVM main = Application.Current.MainWindow.Resources["Main"] as MainVM;
                if (main != null)
                    main.Requests.Add(Request);
            }
            else
            {
                DBCommands.UpdateRequest(Request);
                _EditVM.Copy(Request);
            }
        }
        #endregion

        #region Constructor
        public AddRequestVM()
        {
            _AddCommand = new DelegateCommand(new Action<object>(AddExecuted));
            if (!App.IsInDesignMode)
            {
                Hair = DBCommands.GetHair();
                Request.Hair = Hair[0];
                HairTypes = DBCommands.GetHairTypes();
                Request.HairType = HairTypes[0];
                Eyes = DBCommands.GetEyes();
                Request.Eye = Eyes[0];
                Skin = DBCommands.GetSkin();
                Request.Skin = Skin[0];
                Gender = DBCommands.GetGender();
                Request.Gender = Gender[0];
                Types = DBCommands.GetTypes();
                Request.Type = Types[0];
                Status = DBCommands.GetStatus();
                Request.Customer.Status = Status[0];
                Locations = DBCommands.GetLocations();
                Request.Customer.Location = Locations[0];
                Request.Date = DateTime.Now;
                Request.Comment = "";

                // Hookup to customer changes to do auto-lookups
                Request.Customer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Customer_PropertyChanged);
            }
        }

        void Customer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Email" && _EditVM == null)
            {
                Request.Customer.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Customer_PropertyChanged);
                CustomerVM cust = DBCommands.FindCustomer(Request.Customer.Email);
                if (cust != null)
                {
                    Request.Customer.Copy(cust);
                }
                else
                {
                    string email = Request.Customer.Email;
                    Request.Customer = new CustomerVM();
                    Request.Customer.Email = email;
                    Request.Customer.Location = Locations[0];
                }
                Request.Customer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Customer_PropertyChanged);
            }
        }
        #endregion

        public void Edit(RequestVM edit)
        {
            Request.Customer.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Customer_PropertyChanged);
            Request.Copy(edit);
            Request.Customer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Customer_PropertyChanged);
            _EditVM = edit;
        }
    }
}
